package com.tengju.bff.interfaces.covercharge.service.bestsign.convert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.tengju.bff.interfaces.covercharge.entity.vo.contract.*;
import com.tengju.bff.interfaces.covercharge.service.bestsign.BestSignClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liang_shi
 * @date 2020/7/13 15:27
 * @description 合同类
 */
public class BestSignContractConverter {


    /**
     * @param tid 获取模版变量
     * @return 业务报文
     */
    public static Map<String, Object> packageGetTemplateVarsReq(String tid) {
        //1.业务报文
        Map<String, Object> bizMap = Maps.newHashMap();
        bizMap.put("tid", tid);
        bizMap.put("isRetrieveAllVars", "0");
        return bizMap;
    }


    /**
     * @param data data数据
     * @return 模板变量
     */
    public static List<TemplateVarsVo> packageGetTemplateVarsRes(String data) {
        HashMap varsDateMap = JSON.parseObject(data, HashMap.class);
        String templateValues = String.valueOf(varsDateMap.get("templateVars"));
        return JSON.parseArray(templateValues, TemplateVarsVo.class);
    }


    /**
     * 通过模板生成合同文件
     *
     * @param tid 模板编号
     * @return 业务报文
     */
    public static Map<String, Object> packageGetCreateContractTokenReq(String tid, JSONObject values, String groups,String account) {
        //1.业务报文
        Map<String, Object> bizMap = Maps.newHashMap();
        bizMap.put("account", account);
        bizMap.put("tid", tid);
        bizMap.put("templateValues", values);
        bizMap.put("groupValues", groups);
        return bizMap;
    }


    /**
     * @param req 通过模板创建合同
     * @return 业务报文
     */
    public static Map<String, Object> packageCreateContractReq(ContractCreateReq req,String account) {
        //1.业务报文
        Map<String, Object> bizMap = Maps.newHashMap();
        bizMap.put("account", account);
        bizMap.put("tid", req.getTid());
        bizMap.put("templateValues", req.getTemplateValues());
        bizMap.put("groupValues", req.getGroupValues());
        bizMap.put("title", req.getTitle());
        bizMap.put("description", req.getDescription());
        bizMap.put("expireTime", req.getExpireTime());
        bizMap.put("hotStoragePeriod", req.getHotStoragePeriod());
        return bizMap;
    }

    /**
     * 创建合同结果
     *
     * @param data data数据
     * @return 合同Id
     */
    public static String packageCreateContractRes(String data) {
        HashMap contractMap = JSON.parseObject(data, HashMap.class);
        return String.valueOf(contractMap.get("contractId"));
    }

    /**
     * 生成用户签名/印章图片
     *
     * @param req 请求
     * @return 业务报文
     */
    public static Map<String, Object> packageCreateUserSignatureImageReq(CreateUserSignatureReq req) {
        //1.业务报文
        Map<String, Object> bizMap = Maps.newHashMap();
        bizMap.put("account", req.getAccount());
        bizMap.put("text", req.getText());
        bizMap.put("fontName", req.getFontName());
        bizMap.put("fontSize", req.getFontSize());
        bizMap.put("fontColor", req.getFontColor());
        return bizMap;
    }


    /**
     * 生成企业印章图片
     *
     * @param req 请求
     * @return 业务报文
     */
    public static Map<String, Object> packageCreateCompanySignatureImageReq(CreateCompanySignatureReq req) {
        //1.业务报文
        Map<String, Object> bizMap = Maps.newHashMap();
        bizMap.put("account", req.getAccount());
        bizMap.put("text", req.getText());
        bizMap.put("footText", req.getFootText());
        bizMap.put("footText2", req.getFootText2());
        return bizMap;
    }

    /**
     * 用模版变量签署合同
     *
     * @param req 请求
     * @return 业务报文
     */
    public static Map<String, Object> packageAutoSignContractReq(AutoSignContractReq req) {
        //1.业务报文
        Map<String, Object> bizMap = Maps.newHashMap();
        bizMap.put("contractId", req.getContractId());
        bizMap.put("tid", req.getTid());
        bizMap.put("vars", req.getVars());
        bizMap.put("loginIp", req.getLoginIp());
        bizMap.put("signIp", req.getSignIp());
        bizMap.put("loginTime", req.getLoginTime());
        bizMap.put("signatureImageWidth",req.getSignatureImageWidth());
        bizMap.put("signatureImageHeight",req.getSignatureImageHeight());
        return bizMap;
    }

    /**
     * 用模版变量的手动签
     *
     * @param req 请求
     * @return 业务报文
     */
    public static Map<String, Object> packageHandSignContractReq(HandSignContractReq req) {
        //1.业务报文
        Map<String, Object> bizMap = Maps.newHashMap();
        bizMap.put("contractId", req.getContractId());
        bizMap.put("tid", req.getTid());
        bizMap.put("signatureImageHeight", req.getSignatureImageHeight());
        bizMap.put("signatureImageWidth", req.getSignatureImageWidth());
        bizMap.put("hwr", req.getHwr());
        bizMap.put("expireTime", req.getExpireTime());
        bizMap.put("varNames", req.getVarNames());
        bizMap.put("signer", req.getSigner());
        bizMap.put("signatureImageData", req.getSignatureImageData());
        bizMap.put("loginIp", req.getLoginIp());
        bizMap.put("signIp", req.getSignIp());
        bizMap.put("loginTime", req.getLoginTime());
        // 1点击签名图片能触发手写面板-"1"
        bizMap.put("isDrawSignatureImage", req.getIsDrawSignatureImage());
        //"default"
        bizMap.put("signatureImageName", req.getSignatureImageName());
        bizMap.put("pushUrl", req.getPushUrl());
        //“3”-v3版本，UI优化，移动端手绘面板自动横屏
        bizMap.put("version", req.getVersion());
        bizMap.put("vcodeMobile", req.getVcodeMobile());
        bizMap.put("vcodeMail", req.getVcodeMail());
        bizMap.put("returnUrl", req.getReturnUrl());
        bizMap.put("isShowHandwrittenTime", req.getIsShowHandwrittenTime());
        bizMap.put("isAllowChangeSignaturePosition", req.getIsAllowChangeSignaturePosition());
        bizMap.put("isFaceAuth", req.getIsFaceAuth());
        bizMap.put("forceReadingSeconds", req.getForceReadingSeconds());
        //枚举值0/1，默认为0 0-无需拖动到页面底部即可确认签署 1-必须拖动到页面底部，表示阅读完毕才能确认签署
        bizMap.put("readAll", req.getReadAll());
        return bizMap;
    }

    /**
     * 手动签结果
     *
     * @param data data数据
     * @return 手动签结果
     */
    public static HandSignContractRes packageHandSignContractRes(String data) {
        HandSignContractRes res = new HandSignContractRes();
        HashMap contractMap = JSON.parseObject(data, HashMap.class);
        res.setShortUrl(String.valueOf(contractMap.get("shortUrl")));
        res.setShortUrl(String.valueOf(contractMap.get("longUrl")));
        return res;
    }

    /**
     * 锁定并结束
     *
     * @param contractId 合同Id
     * @return 锁定合同请求
     */
    public static Map<String, Object> packageLockContractReq(String contractId) {
        //1.业务报文
        Map<String, Object> bizMap = Maps.newHashMap();
        bizMap.put("contractId", contractId);
        //非必填，默认值为“0”表示无查验二维码，选“1”则每页右下角添加查验二维码
        bizMap.put("qrCode", "0");
        return bizMap;
    }


    /**
     * 查询合同信息
     *
     * @param contractId 合同编号
     * @return 结果
     */
    public static Map<String, Object> packageQueryContractInfoReq(String contractId) {
        //1.业务报文
        Map<String, Object> bizMap = Maps.newHashMap();
        bizMap.put("contractId", contractId);
        return bizMap;
    }


    /**
     * 合同信息
     *
     * @param data data数据
     * @return 合同信息
     */
    public static ContractInfoRes packageContractInfoRes(String data) {
        ContractInfoRes res = new ContractInfoRes();
        HashMap contractMap = JSON.parseObject(data, HashMap.class);
        res.setFid(String.valueOf(contractMap.get("fid")));
        res.setSenderAccount(String.valueOf(contractMap.get("senderAccount")));
        res.setFinishTime(String.valueOf(contractMap.get("finishTime")));
        res.setDescription(String.valueOf(contractMap.get("description")));
        res.setTitle(String.valueOf(contractMap.get("title")));
        res.setSendTime(String.valueOf(contractMap.get("sendTime")));
        res.setSigners(String.valueOf(contractMap.get("signers")));
        res.setDeveloperId(String.valueOf(contractMap.get("developerId")));
        res.setExpireTime(String.valueOf(contractMap.get("expireTime")));
        res.setPages(String.valueOf(contractMap.get("pages")));
        res.setContractId(String.valueOf(contractMap.get("contractId")));
        res.setIsDel(String.valueOf(contractMap.get("isDel")));
        res.setStatus(String.valueOf(contractMap.get("status")));
        res.setUserId(String.valueOf(contractMap.get("userId")));
        return res;
    }


    /**
     * @param contractId 获取合同预览页URL
     * @return 业务报文
     */
    public static Map<String, Object> packageGetContractPreviewUrlReq(String contractId, String account) {
        //1.业务报文
        Map<String, Object> bizMap = Maps.newHashMap();
        bizMap.put("account", account);
        bizMap.put("contractId", contractId);
        //枚举值：96-低清（默认），120-普清， 160-高清，240-超清
        bizMap.put("dpi", "240");
        bizMap.put("expireTime", null);
        return bizMap;
    }


    /**
     * @param req 下载合同请求
     * @return 业务报文
     */
    public static String packageContractDownloadReq(String req) {
        //1.业务报文
        return "contractId=" + req;
    }


    /**
     * @param req 请求信息
     * @return 业务报文
     */
    public static Map<String, Object> packageUpdateContractImageReq(CreateCompanySignatureReq req) {
        //1.业务报文
        Map<String, Object> bizMap = Maps.newHashMap();
        bizMap.put("account", req.getAccount());
        //图片内容
        bizMap.put("imageData", req.getImageData());
        //签名/印章图片名称
        bizMap.put("imageName", req.getImageName());
        return bizMap;
    }

}
